<?php
require('common_view_functions.php');

$tag_list_js = '';
foreach($tag_list as $tl) {
	$tag_list_js .= '"' . $tl->tag_text . '",';
}
$tag_list_js = substr($tag_list_js, 1, strlen($tag_list_js) - 3); // need to clear leading and trailing " to stop js errors in some IDE
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog 用户后台管理 - 编辑网志</title>
<link rel="shortcut icon" href="/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/mgmt.css" />
<link rel="stylesheet" type="text/css" href="/css/3rd/jqModal.css" />
</head>
<body id="bodyEntries">
<div id="wrapper">
	<!-- header -->
	<?php require('common_header.php'); ?>
	<!-- /header -->
	<div id="body">
		<div id="side">
			<div id="btnReturnToListDiv" class="sim_button"><a href="<?php echo('/' . $blogger_uri . '/mgmt/entries'); ?>">返回网志列表</a></div>
			<div id="fileUploadNg">
				<fieldset>
					<legend>拖拽式图片上传</legend>
					<div id="dndDropbox">Drop Zone</div>
					<div id="dndPreview"></div>
					<input type="button" id="btnDnDUpload" value=" 上 传 " class="button" />
					<input type="button" id="btnDnDClear" value="清空" class="button" />
				</fieldset>
			</div>
		</div>
		<div id="main">
			<form id="frmEditEntry">
				<table id="tblEntry" cellspacing="0">
					<tr>
						<td class="label"><label for="txtEntryTitle">标题：</label></td>
						<td>
							<input type="text" id="txtEntryTitle" size="64" maxlength="128" class="textBox" tabindex="11" style="width:38.5em;background-image:url(/img/expr/E<?php echo($entry_data->expression); ?>.gif);" value="<?php echo(htmlspec($entry_data->title)); ?>" />
							<div id="exprDiv">
								↑表情：
								<?php for($i = 1; $i <= 14; $i++) { ?>
								<a rel="expr_<?php echo($i); ?>" href="javascript:;"><img src="<?php echo('/' . 'img/expr/E' . $i . '.gif'); ?>" /></a>
								<?php } ?>
							</div>
						</td>
					</tr>
					<tr>
						<td class="label"><label for="txtEntryContent">正文：</label></td>
						<td>
							<div id="editorToolBar">
								<script type="text/javascript" src="/js/3rd/js_quicktags_mod.js"></script>
								<script type="text/javascript">edToolbar("txtEntryContent");</script>
								<a href="javascript:;" id="lnkInsertBB">蓝色包子图标</a>
								|
								<a href="javascript:;" id="lnkInsertPix">图片上传...</a>
								<div id="emoDiv">
									<table id="tblEmo" style="display:none;left:0;">
										<tr>
											<td><img alt="BB1" src="/img/bbx/BB1.gif" /></td>
											<td><img alt="BB2" src="/img/bbx/BB2.gif" /></td>
											<td><img alt="BB3" src="/img/bbx/BB3.gif" /></td>
											<td><img alt="BB4" src="/img/bbx/BB4.gif" /></td>
											<td><img alt="BB5" src="/img/bbx/BB5.gif" /></td>
											<td><img alt="BB6" src="/img/bbx/BB6.gif" /></td>
											<td><img alt="BB7" src="/img/bbx/BB7.gif" /></td>
										</tr>
										<tr>
											<td><img alt="BB8" src="/img/bbx/BB8.gif" /></td>
											<td><img alt="BB9" src="/img/bbx/BB9.gif" /></td>
											<td><img alt="BB10" src="/img/bbx/BB10.gif" /></td>
											<td><img alt="BB11" src="/img/bbx/BB11.gif" /></td>
											<td><img alt="BB12" src="/img/bbx/BB12.gif" /></td>
											<td><img alt="BB13" src="/img/bbx/BB13.gif" /></td>
											<td><img alt="BB14" src="/img/bbx/BB14.gif" /></td>
										</tr>
										<tr>
											<td><img alt="BB15" src="/img/bbx/BB15.gif" /></td>
											<td><img alt="BB16" src="/img/bbx/BB16.gif" /></td>
											<td><img alt="BB17" src="/img/bbx/BB17.gif" /></td>
											<td><img alt="BB18" src="/img/bbx/BB18.gif" /></td>
											<td><img alt="BB19" src="/img/bbx/BB19.gif" /></td>
											<td><img alt="BB20" src="/img/bbx/BB20.gif" /></td>
											<td><img alt="BB21" src="/img/bbx/BB21.gif" /></td>
										</tr>
										<tr>
											<td><img alt="BB22" src="/img/bbx/BB22.gif" /></td>
											<td><img alt="BB23" src="/img/bbx/BB23.gif" /></td>
											<td><img alt="BB24" src="/img/bbx/BB24.gif" /></td>
											<td><img alt="BB25" src="/img/bbx/BB25.gif" /></td>
											<td><img alt="BB26" src="/img/bbx/BB26.gif" /></td>
											<td><img alt="BB27" src="/img/bbx/BB27.gif" /></td>
											<td><img alt="BB28" src="/img/bbx/BB28.gif" /></td>
										</tr>
										<tr>
											<td><img alt="BB29" src="/img/bbx/BB29.gif" /></td>
											<td><img alt="BB30" src="/img/bbx/BB30.gif" /></td>
											<td><img alt="BB31" src="/img/bbx/BB31.gif" /></td>
											<td><img alt="BB32" src="/img/bbx/BB32.gif" /></td>
											<td>&nbsp;</td>
											<td>&nbsp;</td>
											<td>&nbsp;</td>
										</tr>
									</table>
								</div>
							</div>
							<textarea id="txtEntryContent" class="textBox" cols="64" rows="16" tabindex="12" style="width:40em;height:30em;"><?php echo(htmlspec($entry_data->content)); ?></textarea>
						</td>
					</tr>
					<tr>
						<td class="label"><label for="txtEntryTags">Tag：</label></td>
						<td>
							<input type="text" id="txtEntryTags" class="textBox" size="32" maxlength="256" tabindex="13" style="width:40em;" value="<?php echo($entry_data->tags); ?>" />
							<div id="tagHint">多个 Tag 之间请用<strong>空格</strong>分隔</div>
							<div id="autosuggest"><ul></ul></div>
							<script type="text/javascript">
							</script>
						</td>
					</tr>
					<tr>
						<td class="label"><label>分类：</label></td>
						<td>
							<select id="selEntryCates" class="textBox" tabindex="14">
								<option value="1"<?php if($entry_data->category_id == 1) { ?> selected="selected"<?php } ?>>未分类</option>
								<?php foreach($custom_cates as $cc) { ?>
								<option value="<?php echo($cc->id); ?>"<?php if($entry_data->category_id == $cc->id) { ?> selected="selected"<?php } ?>><?php echo(htmlspec($cc->title)); ?></option>
								<?php } ?>
							</select>
						</td>
					</tr>
					<tr>
						<td class="label"><label>选项：</label></td>
						<td>
							<select id="selEntryState" class="textBox" tabindex="15">
								<option value="1"<?php if($entry_data->state == 1) { ?> selected="selected"<?php } ?>>公开帖</option>
								<option value="3"<?php if($entry_data->state == 3) { ?> selected="selected"<?php } ?>>社区帖</option>
								<option value="0"<?php if($entry_data->state == 0) { ?> selected="selected"<?php } ?>>保密帖</option>
							</select>
							<span id="stateHint">网志状态</span>
							&nbsp;
							<input type="checkbox" id="chkAllowComments" value="nein" tabindex="16"<?php if($entry_data->allow_comment == FALSE) { ?> checked="checked"<?php } ?> />
							<label for="chkAllowComments">关闭评论</label>
						</td>
					</tr>
					<tr>
						<td colspan="2">
							<div id="entryOperButtons">
							<?php if($editing_draft) { ?>
								<input type="button" id="btnSaveDraft" class="button" value="保存草稿" tabindex="17" />
								<input type="button" id="btnPublish" class="button" value="发布网志" tabindex="18" />
							<?php } else { ?>
								<input type="button" id="btnPublish" class="button" value="提交修改" tabindex="17" />
							<?php } ?>
							</div>
						</td>
					</tr>
				</table>
				<input type="hidden" id="hidEntryId" value="<?php echo($entry_data->id); ?>" />
				<input type="hidden" id="hidIsDraftMode" value="<?php echo($editing_draft ? 'j' : 'n'); ?>" />
				<input type="hidden" id="hidReferer" value="<?php echo($http_referer); ?>" />
			</form>
		</div>
		<div class="z"></div>
	</div>
	<!-- footer -->
	<?php require('common_footer.php'); ?>
	<!-- /footer -->
</div>
<div id="uploadPixDiv" class="jqmWindow popout">
	<form id="frmFileUpload">
		<dl>
			<dt>快速图片上传</dt>
			<dd>
				选择图片，上传完毕后复制 UBB 代码，粘贴到正文中需要显示图片的位置即可。
				<ul>
					<li>图片文件名中<span style="color:red;">不能</span>含有<u>空格</u>和<u>中文</u>字符。</li>
					<li>图片宽度最好 <span style="color:red;">≤ 640 像素</span>，大小在 <span style="color:red;">500 KB 以内</span>。</li>
				</ul>
			</dd>
			<dd id="uploadingNotify">正在上传，请稍等...</dd>
			<dd>
				<input type="file" id="filPixUpload" name="filPixUpload" class="textBox" size="16" />
				<input type="button" id="btnPixUpload" class="button" value="上传" />
				<input type="button" id="btnCancelUpload" class="button" value="关闭" />
			</dd>
			<dd>
				<input type="text" id="txtUploadedPixUBB" class="textBox" readonly="readonly" style="width:24em;" />
				<label for="txtUploadedPixUBB">UBB 代码</label>
			</dd>
		</dl>
	</form>
</div>

<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/jqModal.js"></script>
<script type="text/javascript" src="/js/3rd/ajaxfileupload.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript" src="/js/3rd/auto_suggest.js"></script>
<script type="text/javascript" src="/js/dnd-upload.js"></script>
<script type="text/javascript">
<!--
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";
var g_uploadedImagePathPrefix = "<?php echo(base_url() . 'pix/'); ?>";

var entryStateHint = [
	"凭好友密码方可查看", // 0
	"任何人可浏览", // 1
	"", // historic reason...
	"本 Blog 社区注册用户登录后可浏览" // 3
];

var currentEntryId = 0;
var draftMode = true;
var toCreate = true;
var published = false;
var postEntryData = {
	"id": 0,
	"expression": "<?php echo($entry_data->expression); ?>",
	"title": "",
	"category_id": 1,
	"allow_comment": true,
	"state": 99,
	"tags": "",
	"content": ""
};

var autoSaveDirty = false;

var myTags = new Array("<?php echo($tag_list_js); ?>");

function purgeTagString(ts) {
	var ps = jQuery.trim(ts);
	ps = ps.replace(/\s{2,}/ig, " ");
	return ps;
}

function clientValidated(loudly) {
	var entryTitle = $("#txtEntryTitle").val();
	if(entryTitle == "") {
		if(loudly) alert("无论如何也请写上标题，谢谢！");
		return false;
	}
	postEntryData.title = entryTitle;
	
	var entryContent = $("#txtEntryContent").val();
	if(entryContent == "") {
		if(loudly) alert("什么内容都没有？");
		return false;
	}
	postEntryData.content = entryContent;
	
	var entryTags = $("#txtEntryTags").val();
	if(entryTags != "") {
		if(/[^\u4E00-\u9FA0a-zA-Z0-9._ ]/.test(entryTags)) {
			if(loudly) alert("Tag 中不能含有除中文、字母、数字、句点及下划线之外的其他字符！");
			return false;
		}
		
		entryTags = purgeTagString(entryTags);
		var hasSame = false;
		var arrTags = entryTags.split(" ");
		for(var i = 0; i < arrTags.length; i++) {
			for(var j = i + 1; j < arrTags.length; j++) {
				if(arrTags[i] == arrTags[j]) {
					hasSame = true;
					break;
				}
			}
		}
		if(hasSame) {
			if(loudly) alert("Tag 有重复，请修改！");
			return false;
		}
	}
	postEntryData.tags = entryTags;
	
	return true;
}

function autoSaveDraft() {
	if(clientValidated(false)) {
		postEntryData.state = 99; // only drafts can be auto-saved!
		doSaveEntry();
	}
}

function doSaveEntryCalback(rdata) {
	$("#ajaxLoading").hide();
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	if(toCreate) {
		currentEntryId = rdata[1];
		postEntryData.id = rdata[1];
		toCreate = false; // from now on, updating
	}
	if(published) {
		var redirectUrl = $("#hidReferer").val();
		if(redirectUrl != "") {
			location.href = redirectUrl;
		} else {
			location.href = g_userSiteRoot + "/mgmt/entries";
		}
	}
}

function doSaveEntry() {
	postEntryData.category_id = $("#selEntryCates").val();
	var allowComments = true;
	if($("#chkAllowComments:checked").val() == "nein") {
		allowComments = false;
	}
	postEntryData.allow_comment = allowComments;
	
	var postUrl = g_userSiteRoot + "/mgmt/edit_entry";
	if(!toCreate) {
		postUrl = postUrl + "/" + currentEntryId;
	}
	$("#ajaxLoading").show();
	$.post(postUrl, {
		"hidIsPostBack": "ja",
		"posted_data": JSON.stringify(postEntryData)
	}, doSaveEntryCalback);
}

$(document).ready(function() {
	$("#uploadPixDiv").jqm();
	
	$("#txtEntryTitle").focus(function() {
		$("#exprDiv").fadeIn();
	});
	$(":input:not(#txtEntryTitle)").focus(function() {
		$("#exprDiv").fadeOut();
	});
	
	$("#exprDiv a").click(function() {
		var exprId = $(this).attr("rel");
		exprId = exprId.split("_");
		exprId = exprId[1];
		
		postEntryData.expression = exprId;
		$("#txtEntryTitle").css("background-image", "url(/img/expr/E" + exprId + ".gif)");
		$("#txtEntryTitle").focus();
	});
	
	$("#lnkInsertBB").click(function() {
		$("#tblEmo").toggle();
	});
	
	$("#tblEmo img").click(function() {
		var selectedEmo = $(this).attr("alt");
		// using js_quick_tags' code
		edInsertContent("txtEntryContent", "[" + selectedEmo.toLowerCase() + "]");
		$("#tblEmo").hide();
	});
	
	$("#selEntryState").change(function() {
		var state = $(this).val();
		postEntryData.state = state;
		$("#stateHint").text(entryStateHint[state]);
	});
	
	$("#btnSaveDraft").click(function() {
		if(clientValidated(true)) {
			postEntryData.state = 99; // draft!
			doSaveEntry();
		}
	});
	
	$("#btnPublish").click(function() {
		if(clientValidated(true)) {
			if(confirm("确定要发布？")) {
				postEntryData.state = $("#selEntryState").val();
				published = true;
				doSaveEntry();
			}
		}
	});
	
	$("#lnkInsertPix").click(function() {
		// reset upload-box
		$("#filPixUpload").val("");
		$("#txtUploadedPixUBB").val("");
		$("#uploadingNotify").text("正在上传，请稍等...");
		$("#uploadingNotify").hide();
		$("#uploadPixDiv").jqmShow();
	});
	
	$("#btnPixUpload").click(function() {
		return ajaxFileUpload();
	});
	
	$("#btnCancelUpload").click(function() {
		$("#uploadPixDiv").jqmHide();
	});
	
	$("#txtUploadedPixUBB").mouseover(function() {
		$(this).select();
	});
	
	currentEntryId = $("#hidEntryId").val();
	postEntryData.id = currentEntryId;
	draftMode = ($("#hidDraftMode").val() == "j") ? true : false;
	toCreate = (currentEntryId == 0) ? true : false;
	
	$("#stateHint").text(entryStateHint[$("#selEntryState").val()]);
	
	new AutoSuggest(document.getElementById("txtEntryTags"), myTags);
	
	// DnD uploading things
	if("files" in DataTransfer.prototype) { // currently only supports Fx 3.6+
		$("#fileUploadNg").show();
		
		dndDropbox.addEventListener("dragenter", dragenter, true);
		dndDropbox.addEventListener("dragleave", dragleave, true);
		dndDropbox.addEventListener("dragover", dragover, false);
		dndDropbox.addEventListener("drop", drop, false);
		
		$("#btnDnDUpload").click(function() {
			sendFiles();
		});
		
		$("#btnDnDClear").click(function() {
			clearDropped();
		});
	}
});

function ajaxFileUpload() {
	$("#uploadingNotify")
	.ajaxStart(function() {
		$("#ajaxLoading").show();
		$(this).show();
	})
	.ajaxComplete(function() {
		$(this).hide("slow", function() {
			$("#ajaxLoading").hide();
		});
	});

	$.ajaxFileUpload({
		"url": g_userSiteRoot + "/mgmt/upload_pix",
		"secureuri": false,
		"fileElementId": "filPixUpload",
		"dataType": "json",
		"success": function(data, status) {
			if(typeof(data.error) != "undefined") {
				if(data.error != "") {
					alert("Error: " + data.error);
				} else {
					$("#uploadingNotify").html("<span style=\"color:green;\">上传完成。</span>");
					$("#txtUploadedPixUBB").val("[img]<?php echo(base_url() . 'pix/'); ?>" + data.pix + "[/img]");
				}
			}
		},
		"error": function(data, status, e) {
			alert("Oops... " + e);
		}
	});
	
	return false;
}

// -->
</script>
<!-- /JS -->

<div id="ajaxLoading"><img alt="Loading..." src="/img/ajax_loading.gif" border="0" width="24" height="24" /></div>
</body>
</html>